package com.itboye.api.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.itboye.system.crypt.ByDataCrypt;
import com.itboye.system.exception.ByBaseException;
import com.itboye.system.helper.ByConsoleHelper;
import com.itboye.system.helper.ByDateHelper;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;

/**
 * 接口请求过来的参数
 * Created by hebidu on 16/5/10.
 */
public class ByRequestData {



    public HashMap getParams() throws UnsupportedEncodingException {

        ByConsoleHelper.debug("获取的加密的数据"+data,null);
        //从加密的data字符串中获取请求参数

        return ByDataCrypt.decodeData(this.data);
    }

    /**
     * 校验当前数据是否合法
     */
    public void isValid() throws ByBaseException, UnsupportedEncodingException, NoSuchAlgorithmException {

        //1. 请求时间戳校验
        long currentTimestamp = ByDateHelper.getCurrentTimeStamp();

        long elapseTime = currentTimestamp - this.getTimestamp();

        //时间误差15秒以内
        if(elapseTime > 5*60 || elapseTime < -5*60){
            throw  new ByBaseException("请校准您的时间");
        }

        //2. 签名校验
        HashMap<String,String> params = new HashMap<String, String>();

        params.put("client_secret",getClientSecret());
        params.put("notify_id",getNotifyId());
        params.put("time",""+getTimestamp());
        params.put("data",getData());
        params.put("type",getType());
        params.put("alg",getAlg());

        if(!ByDataCrypt.sign(params).equals(this.sign)){
            throw  new ByBaseException("签名校验失败!");
        }

    }



    @Override
    public String toString() {
        return "ByRequestData{" +
                "appVersion='" + appVersion + '\'' +
                ", appType='" + appType + '\'' +
                ", apiVer='" + apiVer + '\'' +
                ", clientId='" + clientId + '\'' +
                ", notifyId='" + notifyId + '\'' +
                ", data='" + data + '\'' +
                ", alg='" + alg + '\'' +
                ", timestamp='" + timestamp + '\'' +
                ", type='" + type + '\'' +
                ", sign='" + sign + '\'' +
                '}';
    }

    public ByRequestData(){

    }

    public ByRequestData(String strRequest){

    }

    public String getAppVersion() {
        return appVersion;
    }

    public void setAppVersion(String appVersion) {
        this.appVersion = appVersion;
    }

    public String getAppType() {
        return appType;
    }

    public void setAppType(String appType) {
        this.appType = appType;
    }

    public String getApiVer() {
        return apiVer;
    }

    public void setApiVer(String apiVer) {
        this.apiVer = apiVer;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    @JsonIgnore
    private String clientSecret;

    private String appVersion;
    private String appType;
    private String apiVer;

    private String clientId;//客户端ID
    private String notifyId;//请求ID

    private String data;//数据,加密过的

    private String alg;//算法

    @JsonProperty("time")
    private Long timestamp;//时间戳

    private String type;//服务类型

    private String sign;//签名

    public String getClientId() {
        return clientId;
    }

    public String getNotifyId() {
        return notifyId;
    }

    public void setNotifyId(String notifyId) {
        this.notifyId = notifyId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getAlg() {
        return alg;
    }

    public void setAlg(String alg) {
        this.alg = alg;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }
}
